from django.db import models


# Create your models here.


class CateModel(models.Model):
    """
    分类表：
        分类名称（cate_name），图片（cate_img）
    """
    cate_name = models.CharField(max_length=30, verbose_name='分类名称')
    cate_img = models.ImageField(upload_to='static/img', verbose_name='分类图片')

    class Meta:
        db_table = 'cate'
        verbose_name = '商品分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.cate_name


"""
轮播图的批号：
第一批

轮播图的图片
批号：外键
图片：图片
"""


class CarouselModel(models.Model):
    """
    轮播图号
    """
    carousel = models.CharField(max_length=20, verbose_name='轮播图号')

    class Meta:
        db_table = 'carousel'

    def __str__(self):
        return self.carousel


class PictureModel(models.Model):
    """
    轮播图的图片
    """
    carousel = models.ForeignKey(CarouselModel, on_delete=models.CASCADE, verbose_name='轮播图号')
    picture = models.ImageField(upload_to='static/pic')

    class Meta:
        db_table = 'picture'

    def __str__(self):
        return self.picture.url


class GoodsModel(models.Model):
    """
    商品表：
        商品名称，图片，价格，单位，简介，详情，分类（外键）
    """
    goods_name = models.CharField(max_length=80, verbose_name='商品名称')
    goods_img = models.ImageField(upload_to='static/goods', verbose_name='商品图片')
    goods_price = models.DecimalField(max_digits=7, decimal_places=2, verbose_name='商品价格')
    goods_unit = models.CharField(max_length=8, verbose_name='单位')
    goods_describe = models.CharField(max_length=500, verbose_name='简介')
    goods_content = models.CharField(max_length=500, verbose_name='详情')
    cate = models.ForeignKey(CateModel, on_delete=models.CASCADE, verbose_name='分类')

    class Meta:
        db_table = 'goods'

    def __str__(self):
        return self.goods_name


class UserModel(models.Model):
    """
    用户模块：
            用户名，密码，邮箱
    """
    username = models.CharField(max_length=30, verbose_name='用户名')
    password = models.CharField(max_length=128, verbose_name='密码')
    email = models.EmailField(max_length=60, verbose_name='邮箱')
    status = models.BooleanField(default=0, null=True, blank=True, verbose_name='状态')  # 状态字段

    class Meta:
        db_table = 'user'

    def __str__(self):
        return self.username


class UserTokenModel(models.Model):
    user = models.OneToOneField(UserModel, on_delete=models.CASCADE, verbose_name='用户')  # 一对一字段
    token = models.CharField(max_length=300, verbose_name='用户token值')

    class Meta:
        db_table = 'usertoken'

    def __str__(self):
        return self.token
